//
//  myWindow.m
//  ufo_application_macosx
//
//  Created by czcomt on 12-5-22.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "window.h"

@implementation ufoWindow

- (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag
{
    [super initWithContentRect:contentRect styleMask:aStyle backing:bufferingType defer:flag];
    
    NSMenu *mainMenu = [[NSMenu alloc] initWithTitle: @"MainMenu"];
    NSMenuItem *mi;
    NSMenu *m;
    mi = [mainMenu addItemWithTitle:@"Apple" 
                             action:@selector(onExit:)
                      keyEquivalent:@""];
    
    m = [[NSMenu alloc] initWithTitle:@"Apple"];
    
    // strange hack
    [NSApp performSelector:@selector(setAppleMenu:) withObject: m];
    
    [mainMenu setSubmenu:m forItem:mi];
    mi = [m addItemWithTitle: @"退出"
                      action: @selector(onExit:)
               keyEquivalent: @"Q"];
    
    NSApplication *app = [NSApplication sharedApplication];
    [app setMainMenu: mainMenu];
    

    
    /*NSWindow *window = [[NSWindow alloc] initWithContentRect:NSMakeRect(100, 0, 200, 200)
                                          styleMask:0
                                            backing:NSBackingStoreBuffered
                                              defer:YES];
    
    NSTextField* textField = [[NSTextField alloc] initWithFrame:NSMakeRect(50, 50, 100, 100)];
    [[window contentView] addSubview:textField];
    [window becomeKeyWindow];
    [self addChildWindow:window ordered:NSWindowAbove];
    [window makeKeyAndOrderFront: nil];*/
    //[self makeKeyWindow];
    
    return self;
}

- (void)onExit:(id)sender 
{
    NSApplication *app = [NSApplication sharedApplication];
    [app stop:nil];
}

- (BOOL) canBecomeKeyWindow
{
	return YES;
}

- (BOOL) canBecomeMainWindow
{
	return YES;
}

/*- (BOOL)isMovableByWindowBackground
{
    return YES;
}*/

- (void)close
{
    NSApplication *app = [NSApplication sharedApplication];
   [app stop:nil];
}

- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag
{
    [self makeKeyAndOrderFront: nil];
    return YES;
}

@end
